package org.teremail.smtp;

import static org.teremail.smtp.SessionAttributes.DATA_MODE;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoFilterChain;
import org.apache.mina.common.IoSession;

/**
 * A filter that will route some input data directly to the next filter, 
 * side stepping the delegate filter.  This is used in the SMTP DATA block,
 * where we are not interestined in parsing the text lines of the data block.
 * 
 * @author Michael Barker
 *
 */
public class ConditionalFilter extends IoFilterAdapter {

    private final IoFilter protocolFilter;

    public ConditionalFilter(IoFilter protocolFilter) {
        this.protocolFilter = protocolFilter;
    }

    @Override
    public void messageReceived(NextFilter nextFilter, IoSession session,
            Object message) throws Exception {
        
        Boolean isDataMode = (Boolean) session.getAttribute(DATA_MODE);
        
        if (isDataMode != null && isDataMode) {
            nextFilter.messageReceived(session, message);
        } else {
            protocolFilter.messageReceived(nextFilter, session, message);
        }
    }

    @Override
    public void destroy() throws Exception {
        protocolFilter.destroy();
    }

    @Override
    public void exceptionCaught(NextFilter nextFilter, IoSession session,
            Throwable cause) throws Exception {
        protocolFilter.exceptionCaught(nextFilter, session, cause);
    }

    @Override
    public void filterClose(NextFilter nextFilter, IoSession session)
            throws Exception {
        protocolFilter.filterClose(nextFilter, session);
    }

    @Override
    public void filterWrite(NextFilter nextFilter, IoSession session,
            WriteRequest writeRequest) throws Exception {
        protocolFilter.filterWrite(nextFilter, session, writeRequest);
    }

    @Override
    public void init() throws Exception {
        protocolFilter.init();
    }

    @Override
    public void messageSent(NextFilter nextFilter, IoSession session,
            Object message) throws Exception {
        protocolFilter.messageSent(nextFilter, session, message);
    }

    @Override
    public void onPostAdd(IoFilterChain parent, String name,
            NextFilter nextFilter) throws Exception {
        protocolFilter.onPostAdd(parent, name, nextFilter);
    }

    @Override
    public void onPostRemove(IoFilterChain parent, String name,
            NextFilter nextFilter) throws Exception {
        protocolFilter.onPostRemove(parent, name, nextFilter);
    }

    @Override
    public void onPreAdd(IoFilterChain parent, String name,
            NextFilter nextFilter) throws Exception {
        protocolFilter.onPreAdd(parent, name, nextFilter);
    }

    @Override
    public void onPreRemove(IoFilterChain parent, String name,
            NextFilter nextFilter) throws Exception {
        protocolFilter.onPreRemove(parent, name, nextFilter);
    }

    @Override
    public void sessionClosed(NextFilter nextFilter, IoSession session)
            throws Exception {
        protocolFilter.sessionClosed(nextFilter, session);
    }

    @Override
    public void sessionCreated(NextFilter nextFilter, IoSession session)
            throws Exception {
        protocolFilter.sessionCreated(nextFilter, session);
    }

    @Override
    public void sessionIdle(NextFilter nextFilter, IoSession session,
            IdleStatus status) throws Exception {
        protocolFilter.sessionIdle(nextFilter, session, status);
    }

    @Override
    public void sessionOpened(NextFilter nextFilter, IoSession session)
            throws Exception {
        protocolFilter.sessionOpened(nextFilter, session);
    }
}
